package com.roncoo.eshop.inventory.config;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

/**
 * Copyright: Copyright (c) 2019 Hu-Cheng
 * 
 * @ClassName: RedisConfig.java
 * @Description: redis cluster数据源配置
 *
 * @version: v1.0.0
 * @author: hucheng
 * @date: 2019年9月18日 下午5:11:51
 *
 */
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "redis.cluster")
public class RedisConfig {

	/**
	 * redis cluster host
	 */
	private Map<String, String> host;
	
	/**
	 * redis cluster post
	 */
	private Map<String, Integer> port;

	@Bean
	public JedisCluster jedisClusterFactory() {
		Set<HostAndPort> nodes = new HashSet<HostAndPort>();
		for (Map.Entry<String, String> map : host.entrySet()) {
			nodes.add(new HostAndPort(map.getValue(), port.get(map.getKey())));
		}
		log.info(">>>>>>>>>>>>>>> redis cluster master nodes：{}", nodes);
		JedisCluster jedisCluster = new JedisCluster(nodes);
		return jedisCluster;
	}
}
